<?php

/**
 * @Author:             wuchenge
 * @Date:               2025-03-06 10:34:52
 * @Last Modified by:   wuchenge
 */
namespace App\Front\Controllers;

use App\Http\Controllers\ApiController;
use App\Libraries\JwtToken;
use App\Libraries\Utils;
use App\Models\PointRecord;
use App\Models\User;
use App\Models\UserRecord;
use App\Requests\UserLoginRequest;
use App\Transformer\UserTransformer;
use Illuminate\Http\Request;

class UserController extends ApiController
{
    /**
     * @var mixed
     */
    protected $user_transformer;

    /**
     * @param UserTransformer $user_transformer
     */
    public function __construct(UserTransformer $user_transformer)
    {
        $this->user_transformer = $user_transformer;
    }

    /**
     * @param Request $request
     */
    public function info(Request $request)
    {
        $user = $request->get('user');
        $user = User::with(['info', 'info.level'])
            ->withCount(['children'])
            ->find($user->id);

        return $this->response($this->user_transformer->transform($user));
    }

    /**
     * @param Request $request
     */
    public function saveProfile(Request $request)
    {
        $info = [
            'id'        => $request->get('user')->id,
            'avatar'    => $request->input('avatar'),
            'nick_name' => $request->input('nick_name'),
            'telegram'  => $request->input('telegram'),
        ];
        $res = User::saveProfile($info);
        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param Request $request
     */
    public function uploadImages(Request $request)
    {
        $base64_img = $request->input('img');
        if (empty($base64_img)) {
            return $this->failed('图片没有');
        }

        // $base64_img = urldecode($base64_img);
        preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $res);
        if (!isset($res[2])) {
            return $this->failed('不是base64图片');
        }

        //获取图片类型
        $type = $res[2];
        //图片保存路径
        $new_file = date('Ym', time()) . '/';
        //图片名字
        $new_file = $new_file . Utils::getRandomString(13, 5) . '.' . $type;

        $base64_img = base64_decode(str_replace($res[1], '', $base64_img));

        $res = \Storage::disk('public')->put($new_file, $base64_img);
        if (!$res) {
            return $this->failed('上传失败');
        }

        return $this->response([
            'url' => url('/uploads/' . $new_file),
        ]);
    }

    /**
     * @param Request $request
     */
    public function invite(Request $request)
    {
        $user_id = $request->input('user_id', 0);
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);
        $skip    = ($page - 1) * $limit;

        $user_lists = User::with(['info', 'info.level'])
            ->withCount(['children'])
            ->where('pid', $user_id)
            ->skip($skip)
            ->limit($limit)
            ->get();

        $data = $this->user_transformer->transformCollection($user_lists);
        return $this->response($data);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function recordCustom(Request $request)
    {
        $user_id = $request->input('user_id', 0);
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);
        $skip    = ($page - 1) * $limit;

        $data = UserRecord::where('user_id', $user_id)
            ->skip($skip)
            ->limit($limit)
            ->get();

        return $this->response($data);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function recordPoint(Request $request)
    {
        $user_id = $request->input('user_id', 0);
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);
        $skip    = ($page - 1) * $limit;

        $data = PointRecord::where('user_id', $user_id)
            ->skip($skip)
            ->limit($limit)
            ->get();

        return $this->response($data);
    }
}
